package com.atguigu.tingshu.common.delay;

import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBlockingQueue;
import org.redisson.api.RDelayedQueue;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * 延迟消息服务
 *
 * @author QA
 * @date 2025/07/03 10:47
 */
@Slf4j
@Component
public class DelayMsgService {
    @Autowired
    RedissonClient redissonClient;
    public void sendDelayMsg(String topic, String msg, long delayTime) {
        try {
            RBlockingQueue<Object> blockingQueue = redissonClient.getBlockingQueue(topic);
            RDelayedQueue<Object> delayedQueue = redissonClient.getDelayedQueue(blockingQueue);
            delayedQueue.offer(msg, delayTime, TimeUnit.SECONDS);
            log.info("延迟关单发送成功，topic:{},msg:{},delayTime:{}", topic, msg, delayTime);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
